home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 4490 / 4490.xpi / components / scripts / imap.js < prev    next >
Text File  |  2010-01-22  |  4KB  |  135 lines

  1. /**********************************************************
  2. IMAP
  3. **********************************************************/
  4. var name="IMAP";
  5. var supportInboxOnly=true;
  6. var supportShowFolders=true;
  7. var needServer=true;
  8.  
  9. function init(){
  10.   this.initStage=ST_DATA;
  11.   if(!this.server&&this.user.indexOf("@")!=-1)this.server="imap."+this.user.split("@")[1];
  12.   this.seq=0;
  13. }
  14. function processSocket(aData){
  15.   if(aData.match(/ OK/)||aData.indexOf("* ")==0){
  16.     if(!this.process(null,aData))++this.stage;
  17.   }else{
  18.     this.onError();
  19.   }
  20. }
  21. function process(aHttpChannel, aData) {
  22.   switch(this.stage){
  23.   case ST_DATA:
  24.     var ar=this.server.split(":");    
  25.     var port=ar[1]?ar[1]:993;
  26.     this.sock=new SocketReader(this,ar[0],port,port==993);
  27.     return false;
  28.   case ST_DATA+1: 
  29.     var user;
  30.     if(this.user.indexOf("|")!=-1)user=this.user.split("|")[0];
  31.     else user=this.user.split("@")[0];
  32.     this.send("LOGIN "+user+" "+this.password);
  33.     return false;
  34.   case ST_DATA+2:
  35.     this.send("LSUB \"\" \"*\"");
  36.     return false;    
  37.   case ST_DATA+3:
  38.     var re=/LSUB \((.*?)\) ".*" "(.+)"/g;
  39.     var o;
  40.     this.folders=[];
  41.     while ((o = re.exec(aData)) != null){
  42.       if(!o[2].match(/INBOX/i)&&o[1].indexOf("\Noselect")==-1){
  43.         this.folders.push(o[2]);
  44.       }
  45.     }  
  46.     this.inboxDone=false;
  47.     this.newData=0;
  48.     this.folderAr=[];
  49.     this.send("SELECT \"INBOX\"");
  50.     return false;
  51.   case ST_DATA+4:
  52.     this.send("SEARCH UNSEEN");
  53.     return false;
  54.   case ST_DATA+5:
  55.     var fnd=aData.match(/\* SEARCH ?(.*)$/m);
  56.     if(fnd){
  57.       var n=fnd[1]?fnd[1].split(" ").length:0;
  58.       if(this.inboxOnly){
  59.         if(!this.inboxDone){
  60.           this.newData=n;
  61.         }
  62.       }else{
  63.         this.newData+=n;
  64.       }
  65.       if(this.inboxDone&&n>0){
  66.         this.folderAr.push(this.curFolder);
  67.         this.folderAr.push(n);
  68.       }      
  69.       if(!this.inboxDone)this.inboxDone=true;
  70.  
  71.       if(this.folders.length>0){        
  72.         var folder=this.folders.pop();
  73.         var name=folder.match(/&(.+)-/);
  74.         if(name){
  75.           var conv = Components.classes["@mozilla.org/intl/scriptableunicodeconverter"]
  76.                       .createInstance(Components.interfaces.nsIScriptableUnicodeConverter);
  77.           conv.charset="UTF-7";
  78.           name=conv.ConvertToUnicode("+"+name[1]);
  79.         }else name=folder;
  80.         this.curFolder=name;
  81.         this.send("SELECT \""+folder+"\"");
  82.         this.stage-=1;
  83.         return true;
  84.       }
  85.       var cnt=this.main.wuGetVal(this.id,this.user,0);
  86.       cnt=cnt?parseInt(cnt):0;    
  87.       var num=this.newData;      
  88.       if(num>=cnt){
  89.         this.mailCount=num-cnt;
  90.       }else{
  91.         this.main.wuSetVal(this.id,this.user,0,num);
  92.         this.mailCount=0;
  93.       }
  94.       this.mailData=this.getData(aData);
  95.       if(this.showFolders){
  96.         if(this.folderAr)this.mailData.folders=encodeArray(this.folderAr);        
  97.       }          
  98.       delete this.folderAr;
  99.       this.mailData.desc=this.getDesc();        
  100.     }
  101.     this.send("LOGOUT");
  102.     return false;            
  103.   case ST_DATA+6:
  104.     this.sock.close();
  105.     if(this.mailCount<0)this.reset();
  106.     else this.stage=ST_DATA;
  107.     this.main.setState(nsIWebMailNotifier.ST_MAILDATA,this.ind);    
  108.     return true;                
  109.   }  
  110.   this.onError();
  111.   return true;
  112. }
  113. function stop(){
  114.   if(this.sock)this.sock.close();
  115. }
  116. function send(cmd){
  117.   ++this.seq;
  118.   var s=this.seq.toString();
  119.   while(s.length<3)s="0"+s;
  120.   this.sock.write("A"+s+" "+cmd+"\r\n");
  121. }
  122.  
  123. function calcCount(){
  124.   return this.mailCount;
  125. }
  126. function getMailURL(){
  127.   this.main.wuSetVal(this.id,this.user,0,this.newData);
  128.   delete this.newData;  
  129.   this.mailCount=0;
  130.   if(!this.main.newMailsOnly){
  131.     this.mailData.desc=this.getDesc();    
  132.     this.main.setState(nsIWebMailNotifier.ST_MAILDATA,this.ind);
  133.   }  
  134.   return null;
  135. }